﻿/// <reference path=jquery-1.3.2-vsdoc2.js>

var SearchForm = function(form) {
    this.initialize(form);
}

// Static variable
SearchForm.activeOptionContainer = null;

SearchForm.prototype.initialize = function (form) {
    this.form = $('#' + form);

    this.categoryField = new CheckListSelect('ctrlCategory', '#categoryChecklist', this.form);
    this.manufactureField = new CheckListSelect('ctrlManufacture', '#manufactureChecklist', this.form);
    this.viewTypeField = new DropdownListSelect('ctrlViewType', '#viewTypeDropdownList', this.form);
    this.pageSizeField = new DropdownListSelect('ctrlPageSize', '#pageSizeDropdownList', this.form);

    var onFocusHandler = function (event) {
        if (event.target.value == '- Tùy chọn -') {
            event.target.value = '';
            event.target.style.textAlign = 'left';
        }
    }

    var onBlurHandler = function (event) {
        if (trim(event.target.value) == '') {
            event.target.value = '- Tùy chọn -';
            event.target.style.textAlign = 'center';
        }
    }

    $('input.inputstyle').bind('focus', onFocusHandler);
    $('input.inputstyle').bind('blur', onBlurHandler);
    $('input.inputstyle').bind('keypress', this, this.submitSearchForm);

    if ($('#ctrlProductName').val().length > 3) {
        $('#h').val($('#ctrlProductName').val());
    }
    if (parseFloat($('#ctrlPriceFrom').val()) > 0) {
        $('#pf').val($('#ctrlPriceFrom').val());
    }

    var priceTo = 2147484000 - parseFloat($('#ctrlPriceTo').val());
    if (priceTo > 1) {
        $('#pt').val($('#ctrlPriceTo').val());
    }

    var selfObject = this;

    $('#searchFormSubmit').bind('click', selfObject, function (event) {
        var selfObject = event.data;
        data = '?cl=' + selfObject.categoryField.serialize();
        data += '&ml=' + selfObject.manufactureField.serialize();
        data += '&ot=' + trim(selfObject.viewTypeField.serialize());
        data += '&ps=' + trim(selfObject.pageSizeField.serialize());

        $('input.inputstyle').each(function () {
            data += '&' + this.id + '=' + trim(this.value);
        });

        location.href = $('#ctrlSearchPage').val() + data;
        //searchForm.form.submit();
    });
};

SearchForm.prototype.submitSearchForm = function(event) {
    var selfObject = event.data;
    
//    if (typeof event == 'undefined') {
//        event = window.event;
//    }
    if (((event.keyCode == 10) || (event.keyCode == 13))) {
        data = '?cl=' + selfObject.categoryField.serialize();
        data += '&ml=' + selfObject.manufactureField.serialize();
        data += '&ot=' + trim(selfObject.viewTypeField.serialize());
        data += '&ps=' + trim(selfObject.pageSizeField.serialize());

        $('input.inputstyle').each(function() {
            data += '&' + this.id + '=' + trim(this.value);
        });

        location.href = $('#ctrlSearchPage').val() + data;
        //searchForm.form.submit();
        return false;
    }

};
var DropdownListSelect = function(hidden_element_name, dropdownlist, theform) {
    this.hidden_element_name = hidden_element_name;
    this.dropdownlist = $(dropdownlist);
    this.controlElement = this.dropdownlist.find('.checklist-select-control:first');
    this.observer = this.controlElement.bind('mousedown', this, this.loadOnRequest);

    var container_id = dropdownlist + '-options-container';
    $(container_id).find('ul > li').each(function() {
        if ($(this).find('span').text().toLowerCase().indexOf($('#' + hidden_element_name).val()) >= 0) {
            $(dropdownlist).find('.checklist-select-value:first').html($(this).find('span').text());
        }
    });
};

DropdownListSelect.prototype.initialize = function(hidden_element_name, dropdownlist, theform) {
        
};
DropdownListSelect.prototype.loadOnRequest = function(event) {
    var selfObject = event.data;
    var container_id = selfObject.dropdownlist.attr('id') + "-options-container";

    if (SearchForm.activeOptionContainer != null) SearchForm.activeOptionContainer.hide();
    SearchForm.activeOptionContainer = $('#' + container_id);

    selfObject.controlElement.unbind('mousedown', selfObject.loadOnRequest);

    var selectRef = selfObject.dropdownlist.find('.checklist-select:first');
    $('#' + container_id).css({ 'top': selectRef.position().top + selectRef.height() + 1, 'left': selectRef.position().left + 1, 'width': selectRef.width() + 3 });

    $('#' + container_id).find('ul > li').click(function() {
        selectRef.find('.checklist-select-value:first').html($(this).find('span').html());
        $('#' + container_id).hide();
        selfObject.observer = selfObject.controlElement.bind('mousedown', selfObject, selfObject.loadOnRequest);
    });
    $('#' + container_id).show();
};
DropdownListSelect.prototype.serialize = function() {
    return this.dropdownlist.find('.checklist-select:first').text();
};

var CheckListSelect = function(hidden_element_name, checklist, theform) {
    this.hidden_element_name = hidden_element_name;
    this.checklist = $(checklist);
    this.controlElement = this.checklist.find('.checklist-select-control:first');
    this.observer = this.controlElement.bind('mousedown', this, this.loadOnRequest);

    var selfObject = this;
    var container_id = selfObject.checklist.attr('id') + "-options-container";

    $('#' + container_id).find('a.btn-apply').bind('click', selfObject, function() {
        $('#' + container_id).hide();
        selfObject.observer = selfObject.controlElement.bind('mousedown', selfObject, selfObject.loadOnRequest);
    });

    $('#' + container_id).find('a.checkAll').bind('click', selfObject, function() {
        var str = '';
        $('#' + container_id).find(':checkbox').each(function() {
            this.checked = true;
            if (this.checked == true) {
                str = str.concat($(this).next('label').text()).concat(',');
            }
        });
        if (str.length > 0) {
            str = str.slice(0, str.length - 1);
        }
        else {
            str = '- Tùy chọn -';
        }

        selfObject.checklist.find('span.checklist-select-value:first').text(str);
    });

    $('#' + container_id).find('a.uncheckAll').bind('click', selfObject, function() {
        $('#' + container_id).find(':checkbox').each(function() {
            this.checked = false;
        });
        selfObject.checklist.find('span.checklist-select-value:first').text('- Tùy chọn -');
    });

    $('#' + container_id).find(':checkbox').change(function() {
        var str = '';
        $('#' + container_id).find(':checkbox').each(function(i) {
            if (this.checked == true) {
                str = str.concat($(this).next('label').text()).concat(',');
            }
        });
        if (str.length > 0) {
            str = str.slice(0, str.length - 1);
        }
        else {
            str = '- Tùy chọn -';
        }

        selfObject.checklist.find('span.checklist-select-value:first').text(str);
    });

    var values = String($('#' + hidden_element_name).val()).split(',');
    for (var i = 0; i < values.length; i++) {
        var o = $('#' + container_id).find(':checkbox[value=\'' + values[i] + '\']');
        if (o.length > 0) {
            o[0].checked = true;
            o.change();
        }
    }
};

CheckListSelect.prototype.loadOnRequest = function(event) {
    var selfObject = event.data;
    if (SearchForm.activeOptionContainer != null) SearchForm.activeOptionContainer.hide();
    var container_id = selfObject.checklist.attr('id') + "-options-container";
    SearchForm.activeOptionContainer = $('#' + container_id);

    selfObject.controlElement.unbind('mousedown', selfObject.loadOnRequest);

    var selectRef = selfObject.checklist.find('.checklist-select:first');
    $('#' + container_id).css({ 'top': selectRef.position().top + selectRef.height() + 1, 'left': selectRef.position().left + 1, 'minWidth': selectRef.width() * 3 });

    $('#' + container_id).show();
};

CheckListSelect.prototype.preLoadOnRequest = function() {
    var container_id = this.checklist.id + "-options-container";
    this.controlElement.unbind('mousedown', this.loadOnRequest);
};

CheckListSelect.prototype.showProgressIndicator = function() {

};

CheckListSelect.prototype.hideProgressIndicator = function() {
    $('#progress_indicator').remove()
};

CheckListSelect.prototype.weave = function() {

};

CheckListSelect.prototype.serialize = function() {
    var container_id = this.checklist.attr('id') + "-options-container";
    var data = new Array();
    $('#' + container_id).find(':checkbox').each(function() {
        if (this.checked == true) {
            data.push(this.value);
        }
    });
    return data.toString();
};

CheckListSelect.prototype.fillPreselectedOptions = function() {


};

CheckListSelect.prototype.refreshValue = function() {
    var labels = [];
    if (this.listOptions) {
        this.listOptions.each(function(option) {
            if (option.checked == true) {
                labels.push(option.up('label').innerHTML.stripTags());
            }
        });
        if (labels.length > 0) {
            this.valueElement.innerHTML = labels.join(', ');
            this.valueElement.style.textAlign = 'left';
        } else {
            this.valueElement.innerHTML = '- Any ' + this.controlElement.title + ' -';
            this.valueElement.style.textAlign = 'center';
        }
    }

};

CheckListSelect.prototype.showList = function() {

};

CheckListSelect.prototype.hideList = function() {

};

CheckListSelect.prototype.toggleList = function() {
    //    Element.visible(this.listElement) ? this.hideList(event) : this.showList(event);
};

CheckListSelect.prototype.checkAll = function() {

};

CheckListSelect.prototype.uncheckAll = function() {

};

CheckListSelect.prototype.onChange = function() {

};

